import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import jwm.event.Dispatcher;
import jwm.event.Event;
import jwm.event.EventDispatcher;
import jwm.event.EventListener;
import jwm.event.URLLoaderEvent;
import jwm.network.ThreadLoader;


public class Checker extends Thread implements Dispatcher {

   private EventDispatcher eventDispatcher;
   private ThreadLoader loader;
   private URL url = null;
   public boolean alive = true;
   
   public Checker() throws InterruptedException, MalformedURLException {
      eventDispatcher = new EventDispatcher();
      url = new URL("http://graph.facebook.com/thehundreds");
      start();
   }
   
   
   
   @Override
   public void dispatchEvent(Event event) {
      eventDispatcher.dispatchEvent(event);
   }

   @Override
   public void addEventListener(EventListener listener) {
      eventDispatcher.addEventListener(listener);
   }

   @Override
   public void removeEventListener(Object listener) {
      eventDispatcher.removeEventListener(listener);
   }



   @Override
   public void run() {
      while (alive) {
         loader = new ThreadLoader();
         loader.load(url);
         loader.addEventListener(new EventListener() {
            @Override
            public void receiveEvent(Event event) {
               String data = ((URLLoaderEvent)event).getData();
               int first = data.indexOf("likes");
               data = data.substring(first + 7);
               data = data.substring(0, data.indexOf(','));
               int count = Integer.valueOf(data);
               if (count > 99990) Main.main.fast();
               if (count > 99998) Main.main.like();
               System.out.println(count);
            }
         });
         if (Main.isSlow)
            try {
               sleep (5000);
            } catch (InterruptedException e) {}
      }
   }

}
